package xcmg.device.service.sale;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.sale.SaleReverseDO;
import xcmg.device.dao.entity.sale.SaleReverseDetailDO;
import xcmg.device.dao.mapper.sale.SaleReverseDetailMapper;
import xcmg.device.dao.mapper.sale.SaleReverseMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.sale.SaleReverseDetailVO;
import xcmg.device.service.vo.sale.SaleReverseVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class SaleReverseDetailService {

    @Autowired
    private SaleReverseDetailMapper saleReverseDetailMapper;

    @Autowired
    private SaleReverseMapper saleReverseMapper;

    @Autowired
    private FastGenClient fastGenClient;

    /**
     * @Author: WangKe
     * @Description: 新增冲销入库详情记录
     * @Date: 2018/3/6 0006
     */
    public ErrorCode add(SaleReverseDetailVO saleReverseDetailVO){
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return ErrorCode.IllegalArument;
        }
        SaleReverseDetailDO saleReverseDetailDO = new SaleReverseDetailDO();
        BeanUtils.copyProperties(saleReverseDetailVO, saleReverseDetailDO);

        saleReverseDetailDO.setId(textResult.getValue());
        saleReverseDetailDO.setState(0);
        saleReverseDetailDO.setCreateDate(new Date());
        saleReverseDetailDO.setRealAmount(BigDecimal.ZERO);

        ErrorCode code = saleReverseDetailMapper.insert(saleReverseDetailDO)>0? ErrorCode.Success: ErrorCode.Failure;
        return code;
    }

    /**
     * @Author: WangKe
     * @Description: 新增入库记录时，更新冲销入库单和详情的状态
     * @Date: 2018/3/7 0007
     */
    public boolean updateByInbound(String reverseOrder, String materialNo, BigDecimal realAmount,
                                   Integer singelState, Integer totalState, String orgId)throws Exception{
        SaleReverseDetailVO detailVO = new SaleReverseDetailVO();
        detailVO.setReverseOrder(reverseOrder);
        detailVO.setMaterialNo(materialNo);
        detailVO.setOrgId(orgId);
        List<SaleReverseDetailDO> list = saleReverseDetailMapper.list(detailVO);
        if(ListUtil.isNullOrEmpty(list)){
            return false;
        }
        SaleReverseDetailDO detailDO = list.get(0);
        if(realAmount.compareTo(detailDO.getAmount()) > 0){
            throw new Exception("实际入库数量不能大于计划入库数量");
        }
        detailDO.setRealAmount(realAmount);
        detailDO.setState(singelState);
        boolean success = saleReverseDetailMapper.updateByPrimaryKeySelective(detailDO)>0;
        if(success){
            SaleReverseVO inboundVO = new SaleReverseVO();
            inboundVO.setReverseOrder(reverseOrder);
            inboundVO.setOrgId(orgId);
            List<SaleReverseDO> inboundList = saleReverseMapper.list(inboundVO);
            if(ListUtil.isNullOrEmpty(inboundList)){
                return false;
            }
            SaleReverseDO inboundDO = inboundList.get(0);
            inboundDO.setState(totalState);
            success = saleReverseMapper.updateByPrimaryKeySelective(inboundDO)>0;

//            if(success && totalState == 2){ //冲销入库完成，调用SAP冲销接口
//                sapCommonService.sapSaleReverse(inboundDO.getDeliveryNoteOrder(), orgId);
//            }
        }
        return success;
    }
}
